package components;

import common.Constant;
import operator.Handler;
import common.Singleton;
import view.ViewControl;

import javax.swing.*;
import javax.swing.border.MatteBorder;
import java.awt.*;
/**
 * 负责显示不同的弹出菜单
 * @author 柴家琪
 *
 */
public class MPop extends JPopupMenu{
    private static final long serialVersionUID = 1L;
    public static final int Real=1,Hidden=-1;
    public MPop(int order){
        if(order==Real) {
            addMenuItem("D_Main","D_Vice","Sep","SelfSelect");
            setBorder(new MatteBorder(2, 0, 2, 0, new Color(127, 255, 0)));
        }else if(order==Hidden) {
            addMenuItem("H_Main", "Sep", "H_Vice");
            setBorder(new MatteBorder(2, 0, 2, 0, Color.MAGENTA));
        }
    }
    private void addMenuItem(String...strings){
        int order;
        String str, toolTipText, iconName;
        for (String string : strings) {
            iconName = "";
            toolTipText = "";
            switch (string) {
                // Disappear multiply Locations
//				case "Article":
//					order = MItem.Article;
//					str = "全文选择";
//					break;
//				case "Paragraph":
//					order = MItem.Paragraph;
//					str = "全段选择";
//					toolTipText = "选取全段中相同字段";
//					break;
                case "SelfSelect":
                    order = MItem.SelfSelect;
                    str = "选取多意象";
                    iconName = "cherry.png";
                    toolTipText = "手动选取多个意象，并保存在一条链中，第一个是主意象";
                    break;
                case "H_Vice":
                    order = MItem.H_Vice;
                    str = "增加隐含副意象";
                    iconName = "green_carrot.png";
                    toolTipText = "添加副意象，保存在主意象的链下";
                    break;
                case "D_Main":
                    order = MItem.Direct_Main;
                    str = "添加主意象";
                    iconName = "orange.png";
                    break;
                case "D_Vice":
                    order = MItem.Direct_Vice;
                    str = "添加副意象";
                    iconName = "green_orange.png";
                    toolTipText = "添加隐含的副意象，保存在主意象的链下";
                    break;
                case "H_Main":
                    order = MItem.H_Main;
                    str = "增加隐含主意象";
                    iconName = "carrot.png";
                    toolTipText = "添加一个隐含的主意象";
                    break;
                case "Sep":
                    this.addSeparator();
                    continue;
                default:
                    order = -1;
                    str = "";
            }
            add(new MItem(order, str, toolTipText, iconName));
        }
    }
}
class MItem extends JMenuItem{
    private static final long serialVersionUID = 1L;
    static final int Article=1, Paragraph =2,SelfSelect=3,H_Vice=4,Direct_Main=6,Direct_Vice=7,H_Main=41;

    MItem(int order, String text, String toolTipText, String iconName){
        Handler handler = Singleton.handler();
        ViewControl vc = Singleton.vc();

        setText(text);
        setFont(new Font(Constant.boldfaceName, Font.BOLD,14));

        if (!toolTipText.equals("")) {
            setToolTipText(toolTipText);
        }

        if (!iconName.equals("")) {
            setIcon(Singleton.resource().getIcon(iconName));
        }

        addActionListener(e -> {
            switch(order){
                // Disappear multiply Locations
//            case Article:
//                handler.showEveryOfSelected();
//                vc.showMenus_New(false);
//                break;
//            case Paragraph:
//                handler.showEachInP();
//                vc.showMenus_New(false);
//                break;
                case SelfSelect:
                    handler.selfSelect();//根据选择的情况变更菜单
                    vc.showMenus_New(true);
                    break;
                case Direct_Main:
                    handler.directAdd(true);break;
                case Direct_Vice:
                    handler.directAdd(false);break;
                case H_Vice:
                    vc.showSelector(true);
                    break;
                case H_Main:
                    handler.directAdd(true);break;
            }
        });

    }
}
